package com.zuohuaipan.headine.adapter;

import android.content.Context;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.zuohuaipan.headine.R;
import com.zuohuaipan.headine.bean.Video_Bean;
import com.zuohuaipan.headine.utils.Xutils_image;
import com.zuohuaipan.headine.utils.Xutils_video_image;

import java.util.ArrayList;

import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;

/**
 * Created by ASUS on 2017/1/13.
 */

public class Video_Adapter extends BaseAdapter {
    Context context;
    ArrayList<Video_Bean> list;
    private LayoutInflater inflater;

    public Video_Adapter(FragmentActivity activity, ArrayList<Video_Bean> arrayList) {
        context = activity;
        list = arrayList;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder vh = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.video_item, parent, false);
            vh = new ViewHolder();
            vh.jcvideo = (JCVideoPlayerStandard) convertView.findViewById(R.id.jcvideoplayer);
            vh.tv_title = (TextView) convertView.findViewById(R.id.video_name);
            vh.tv_num = (TextView) convertView.findViewById(R.id.video_num);
            vh.iv_img = (ImageView) convertView.findViewById(R.id.video_img);
            vh.iv_fx = (ImageView) convertView.findViewById(R.id.video_image);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        //赋值
        vh.jcvideo.setUp(list.get(position).mp4_url, list.get(position).title);
        Xutils_image.display(vh.jcvideo.thumbImageView, list.get(position).cover);
        vh.jcvideo.thumbImageView.setScaleType(ImageView.ScaleType.FIT_XY);
        //title赋值
        if (list.get(position).topicName != null) {
            vh.tv_title.setText(list.get(position).topicName);
        } else {
            vh.tv_title.setText("精品");
        }
        //播放次数赋值
        if (list.get(position).playCount == null) {
            vh.tv_num.setText("0次播放");
        } else {
            vh.tv_num.setText(list.get(position).playCount + "次播放");
        }
        //头像赋值
        if (list.get(position).topicImg != null) {
            Xutils_video_image.display(vh.iv_img, list.get(position).topicImg);
        } else {
            vh.iv_img.setImageResource(R.drawable.proxy3);
        }
        //监听
        vh.iv_fx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "点击了", Toast.LENGTH_SHORT).show();
            }
        });

        return convertView;
    }

    class ViewHolder {
        JCVideoPlayerStandard jcvideo;
        ImageView iv_img, iv_fx;
        TextView tv_title, tv_num;
    }
}
